//
//  ByWageListVC.m
//  recordworktool_ios
//
//  Created by mc on 2017/12/8.
//  Copyright © 2017年 ZhangZhanbin. All rights reserved.
//

#import "ByWageListVC.h"
#import "ByWageListCell.h"
#import "ByWageHeadView.h"
#import "ByWageListSecondCell.h"
#import "ByWageHeadSecondView.h"

#import "ByAccountDetailVC.h"
@interface ByWageListVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger selectedIndex;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UITextField *searchTF;

@end

@implementation ByWageListVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    
    [self initViews];
    
    [self loadDataWithSearchValueName:nil project:nil];;
    
    NSLog(@"uid = %@", BY_USER.uid);
    // Do any additional setup after loading the view.
}

- (NSString *)getTimeStrWithYear:(NSInteger )year month:(NSInteger )month day:(NSInteger )day{
    
    NSString *monthStr = @"";
    NSString *dayStr = @"";
    
    if (month < 10) {
        monthStr = [NSString stringWithFormat:@"0%ld", month];
    }else{
        monthStr = [NSString stringWithFormat:@"%ld", month];
    }
    if (day == 0) {
        dayStr = @"";
    }else{
        
        if (day < 10) {
            dayStr = [NSString stringWithFormat:@"0%ld", day];
        }else{
            dayStr = [NSString stringWithFormat:@"%ld", day];
        }
    }
    return [NSString stringWithFormat:@"%ld%@%@", year,monthStr,dayStr];
    
}

- (void)loadDataWithSearchValueName:(NSString *)name project:(NSString *)project{
    
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.uid = BY_USER.uid;
    netModel.mobile = BY_USER.mobile;
    netModel.project_title = project;
    netModel.name = name;
    [ByCoreToolAlert showLoadind:@"加载中"];
    //工人
    if (BY_USER.isWorker.integerValue == 1) {
        
        [ByApiRecord queryWorker:netModel success:^(ByNetBaseResult *result){
            
            [ByCoreToolAlert hideHud];
            self.dataArray = result.data;
            [self.tableView reloadData];
            
        } failure:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert showFailureWithText:result.msg];
            
        }];
        
    }else{
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
        NSInteger  year =  components.year;
        NSInteger month = components.month;
        NSString *daytime = [self getTimeStrWithYear:year month:month day:0];
        netModel.daytime = daytime;
        [ByApiRecord queryOverseer:netModel success:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert hideHud];
            self.dataArray = result.data;
            [self.tableView reloadData];
            
        } failure:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
        
    }

}


- (void)initViews{
    NSArray *itemsAry = [NSArray array];
    if (BY_USER.isWorker.integerValue == 1) {
        itemsAry = @[@"按工头",@"按项目"];
    }else{
        itemsAry = @[@"按工人",@"按项目"];
    }
    UISegmentedControl *controlView = [[UISegmentedControl alloc] initWithItems:itemsAry];
    controlView.frame = CGRectMake(0, 0, 200, 30);
    controlView.layer.cornerRadius = 5;
    controlView.selectedSegmentIndex = 0;
    self.selectedIndex = 0;
    controlView.tintColor = [UIColor whiteColor];
    controlView.layer.borderWidth = 2;
    controlView.layer.borderColor = [UIColor whiteColor].CGColor;
    [controlView setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateSelected];
    [controlView setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateNormal];
    [controlView addTarget:self action:@selector(controlValueChanged:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = controlView;
    
    
    UITextField *searchTF = [[UITextField alloc] init];
    [self.view addSubview:searchTF];
    [searchTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.top.equalTo(self.view).offset(20);
        make.height.mas_equalTo(@40);
    }];
    searchTF.layer.borderWidth = 2;
    searchTF.layer.borderColor = [UIColor lightGrayColor].CGColor;
    searchTF.layer.cornerRadius = 20;
    searchTF.layer.masksToBounds = YES;
    searchTF.placeholder = @"搜索框可搜索工头和项目";
    searchTF.font = FONT(14);
    [self setTextFieldLeftPadding:searchTF forWidth:10];
    self.searchTF = searchTF;
    
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
    searchView.backgroundColor = [UIColor whiteColor];
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(0, 0, 20, 20);
    [searchBtn setImage:[UIImage imageNamed:@"fangdajing"] forState:UIControlStateNormal];
    [searchView addSubview:searchBtn];
    [searchBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    
    searchTF.rightView = searchView;
    searchTF.rightViewMode = UITextFieldViewModeAlways;
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,80, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_NAV - 80) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
}


- (void)searchAction:(UIButton *)btn{
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    if (self.searchTF.text.length == 0){
        [self loadDataWithSearchValueName:nil project:nil];
        return;
    }
    
    NSLog(@"搜索");
    
    if (self.selectedIndex == 0) {
        [self loadDataWithSearchValueName:self.searchTF.text project:nil];
    }else{
        [self loadDataWithSearchValueName:nil project:self.searchTF.text];
    }
    
}

-(void)setTextFieldLeftPadding:(UITextField *)textField forWidth:(CGFloat)leftWidth
{
    CGRect frame = textField.frame;
    frame.size.width = leftWidth;
    UIView *leftview = [[UIView alloc] initWithFrame:frame];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftview;
}

- (void)controlValueChanged:(UISegmentedControl *)seg{
    self.selectedIndex = seg.selectedSegmentIndex;
    [self.tableView reloadData];
    self.searchTF.text = @"";
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"按工头");
    }else{
        NSLog(@"按项目");
    }
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    if (self.selectedIndex == 0) {
        ByWageListSecondCell *cell = [ByWageListSecondCell cellWithTableView:tableView];
        cell.netModel = netModel;
        cell.detailAction = ^{
            [[UIApplication sharedApplication].keyWindow endEditing:YES];
            NSLog(@"详情");
            ByAccountDetailVC *vc = [[ByAccountDetailVC alloc] init];
            vc.netModel = netModel;
            vc.type = 0;
            [self by_normalPush:vc hasNavi:YES];
        };
        return cell;
        
    }else{
        
        ByWageListCell *cell = [ByWageListCell cellWithTableView:tableView];
        cell.netModel = netModel;
        cell.detailAction = ^{
            [[UIApplication sharedApplication].keyWindow endEditing:YES];
            NSLog(@"详情");
            ByAccountDetailVC *vc = [[ByAccountDetailVC alloc] init];
            vc.netModel = netModel;
            vc.type = 0;
            [self by_normalPush:vc hasNavi:YES];
        };
        return cell;
    }
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.selectedIndex == 0) {
        
        ByWageHeadSecondView *headView = [[ByWageHeadSecondView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        return headView;
        
    }else{
        
        ByWageHeadView *headView = [[ByWageHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        return headView;
    }
   
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
